# Paper: Is it still the economy? Economic voting in polarized politics
# Individual-level simulation
# Replication: Figure S18 (Supplementary Material)
remove(list = ls())

library(conflicted)
library(tidyverse)
library(dplyr)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(here) # When we have an R-project, it reads files without setting an directory
conflict_prefer("here", "here")
library(nnet)
library(ggeffects)
library(MASS)
conflict_prefer("select","dplyr")

load(here("Final_Paper", "R&R", "IndLevel.RData"))
glimpse(model_data)
table(model_data$partisan)

summary(ml_inc <- MASS::polr(econ_eval ~ percep_polar*partisan + 
                               female + black + hispanic + asian + other_race + 
                               log_age + college + income + state_fips +
                               election, 
                             data = model_data, Hess=TRUE))

summary(ml_inc)
ml_inc2 <- ml_inc %>% 
  ggeffect(terms = c("partisan", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_inc2)
ml_inc2 <- data.frame(ml_inc2)

ml_inc2 <- ml_inc2 %>% 
  rename(partisan = x)
class(ml_inc2)

ml_inc2 <- ml_inc2 %>% 
  dplyr::mutate(econ_eva = base::ifelse(response.level == "X1", "Worse", NA),
                econ_eva = ifelse(response.level == "X2", "Same", econ_eva),
                econ_eva = ifelse(response.level == "X3", "Better", econ_eva))

table(ml_inc2$partisan)
g_inc <- ml_inc2 %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Inpart", 
                                      "LeanIn",
                                      "Ind",
                                      "LeanOut",
                                      "Outpart"),
                           labels = c("President's Party Strong", 
                                      "President's Party Weak", 
                                      "Independent",
                                      "Opposing Party Weak",
                                      "Opposing Party Strong"))) %>% 
  mutate(econ_eva = factor(econ_eva,
                           levels = c("Worse", 
                                      "Same",
                                      "Better"),
                           labels = c("Worse", 
                                      "Same",
                                      "Better"))) %>% 
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(econ_eva == "Worse") %>% 
  #mutate(x = as.factor(x)) %>%
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = "Partisan Attachment", 
       y = "Probability of Negative Economic Evaluation") +
  #scale_color_manual(name = "Partisanship:", 
  #               values = c("gray17", "gray37", "gray57"),
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_shape_manual(name = "Perceived Ideological Gap:",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  #scale_fill_manual(name = "Partisanship:",
  #                 values = c("gray17", "gray37", "gray57"), 
  #                labels = c("Inpart", "Ind", "Outpart")) +
  scale_y_continuous(breaks = c(0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0, 0.8)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 12),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(size = 12),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 14))
g_inc




